//
//  PainterUserDataBase.m
//  p4
//
//  Created by CaoJun on 15/5/25.
//  Copyright (c) 2015年 com.baidao. All rights reserved.
//

#import "PainterUserDataBase.h"

using namespace ytx;

@implementation PainterUserDataBase

- (void) drawLine:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ytx::ViewModelIndexPosition *) indexPosition withUserGraph:(ytx::UserGraph) userGraph withDrawSubline:(BOOL) isDrawSubline;
{

}

- (void) drawRectangle:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ytx::ViewModelIndexPosition *) indexPosition withUserGraph:(ytx::UserGraph) userGraph withDrawSubline:(BOOL) isDrawSubline;
{

}

- (void)drawCrosshairsY:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px
{
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    [ctx setStrokeStyle:[NSString stringWithUTF8String: style.crosshairsColor.c_str() ] ];
    [ctx setLineWidth:style.crosshairsWidth];
    [ctx drawLineWithX1:rect.getBeginX() withY1: px withX2:rect.getEndX() withY2: px];
    
}

- (void)drawCrosshairsYText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px withText:(NSString *) text
{
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    NSString * font = [NSString stringWithUTF8String:style.crosshairsTextFont.c_str()];
    
    CGRect backgroundRect = [ctx getTextRect:text withFont:font withSize:style.crosshairsTextSize];
    backgroundRect.size.width+=2;
    backgroundRect.size.height+=2;
    backgroundRect.origin.x = rect.getBeginX();
    backgroundRect.origin.y = px-backgroundRect.size.height*0.5;
    
    [ctx fillRect:backgroundRect withFillColor:[NSString stringWithUTF8String:style.crosshairsTextBackgorundColor.c_str()]];
    
    [ctx fillText:text withX:rect.getBeginX()+1 withY:px withColor:[NSString stringWithUTF8String:style.crosshairsTextColor.c_str()] withFont:font withSize:style.crosshairsTextSize withAnchor:LeftMiddle];
    
}

- (void)drawCrosshairsXText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ytx::ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px withText:(NSString *) text
{
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    NSString * font = [NSString stringWithUTF8String:style.crosshairsTextFont.c_str()];
    
    CGRect backgroundRect = [ctx getTextRect:text withFont:font withSize:style.crosshairsTextSize];
    backgroundRect.size.width+=2;
    backgroundRect.size.height+=2;
    backgroundRect.origin.x = px-backgroundRect.size.width*0.5;
    backgroundRect.origin.y = rect.getBeginY();
    
    [ctx fillRect:backgroundRect withFillColor:[NSString stringWithUTF8String:style.crosshairsTextBackgorundColor.c_str()]];
    
    [ctx fillText:text withX:px withY:backgroundRect.origin.y withColor:[NSString stringWithUTF8String:style.crosshairsTextColor.c_str()] withFont:font withSize:style.crosshairsTextSize withAnchor:CenterTop];
}

- (void)drawCrosshairsX:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px
{
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    [ctx setStrokeStyle:[NSString stringWithUTF8String: style.crosshairsColor.c_str() ] ];
    [ctx setLineWidth:style.crosshairsWidth];
    [ctx drawLineWithX1:px withY1:rect.getBeginY() withX2:px withY2:rect.getEndY()];
    
}


- (void)drawHoldingPositionInfosWithContext:(ChartCanvas *)ctx viewModelIndexPosition:(ViewModelIndexPosition *)indexPosition
{
    KLineData *kLineData = dynamic_cast<KLineData *>(indexPosition->getData());
    if (!kLineData) {
        return;
    }
    
    const ytx::Style &style = ytx::YTXConfig::getInstance()->style;
    const CGFloat lineWidth = style.buyLineWidth;
    const Rectangle &rect = indexPosition->getRectangle();
    const CGFloat drawX = rect.getEndX() - rect.getPaddingRight();
    const CGFloat x1 = rect.getBeginX();
    const CGFloat x2 = rect.getEndX();
    NSString *longColorStr = [NSString stringWithUTF8String:style.defaultRed.c_str()];
    NSString *shortColorStr = [NSString stringWithUTF8String:style.defaultGreen.c_str()];
    UIColor *longColor = [ChartCanvas colorWithHexString:longColorStr];
    UIColor *shortColor = [ChartCanvas colorWithHexString:shortColorStr];
    UIFont *font = [UIFont systemFontOfSize:style.buyPointTextSize];
    NSString *descTpl = @"%@ %g千克";
    NSString *buyLong = @"做多";
    NSString *buyShort = @"做空";
    const std::list<HoldingPositionInfo> &holdingPositionInfos = kLineData->getHoldingPositionInfos();
    
    for (std::list<HoldingPositionInfo>::const_iterator it = holdingPositionInfos.begin(); it != holdingPositionInfos.end(); ++it) {
        const HoldingPositionInfo &info = *it;
        const double price = std::get<0>(info);
        const double weight = std::get<1>(info);
        const bool isLong = std::get<2>(info);
        
        const CGFloat y = rect.getPxY(price);
        NSString *colorStr = isLong ? longColorStr : shortColorStr;
        [ctx drawDashLineWidth:lineWidth color:colorStr x1:x1 y1:y x2:x2 y2:y];
        
        NSString *direction = isLong ? buyLong : buyShort;
        UIColor *color = isLong ? longColor : shortColor;
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:descTpl, direction, weight]
                                                                                attributes:@{
                                                                                             NSFontAttributeName:font,
                                                                                             NSForegroundColorAttributeName:[UIColor whiteColor],
                                                                                             NSBackgroundColorAttributeName:color
                                                                                             }];
        NSAttributedString *priceAttrStr = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" %g", price]
                                                                           attributes:@{
                                                                                        NSFontAttributeName:font,
                                                                                        NSForegroundColorAttributeName:color
                                                                                        }];
        [str appendAttributedString:priceAttrStr];
        [ctx fillAttributedString:[str copy] atPoint:CGPointMake(drawX, y - 3) anchor:RightBottom];
    }
}

@end
